<template>
  <div class="count-box">
    <button @click="subFn" class="mul">-</button>
    <input :value="value" class="inp" @change="changeValue" type="text">
    <button @click="addFn" class="add">+</button>
  </div>
</template>

<script>
export default {
  name: 'countBox',
  props: {
    value: {
      type: Number,
      default: 1
    }
  },
  methods: {
    subFn () {
      if (this.value > 1) { this.$emit('input', this.value - 1) }
    },
    addFn () {
      this.$emit('input', this.value + 1)
    },
    changeValue (e) {
      if (isNaN(+e.target.value) || +e.target.value < 1) {
        this.$toast('输入不合法')
        e.target.value = this.value
        return
      }
      this.$emit('input', +e.target.value)
    }
  }
}
</script>

<style lang="less" scoped>
    .count-box {
        width: 120px;
        display: flex;
        .mul,.add{
            width: 30px;
            height: 30px;
            background-color: #efefef;
            border: none;
            outline: none;
        }
        .inp{
            width: 50px;
            height: 30px;
            background-color: #efefef;
            margin: 0 5px;
            text-align: center;
            line-height: 30px;
            border: none;
            outline: none;
        }
    }
</style>>
